package com.charzhan.eight;

import com.charzhan.utiles.rabbitmqutil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.util.HashMap;
import java.util.Map;

public class consumer {
    public static final String NORMAL_EXCHANGE="normal_exchange";
    public static final String DEAD_EXCHANGE="dead_exchange";
    public static final String NORMAL_QUEUE="normal_queue";
    public static final String DEAD_QUEUE="dead_queue";
    public static void main(String[] args) throws  Exception{
        Channel channel = rabbitmqutil.getChannel();
        channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);

        channel.queueDeclare(DEAD_QUEUE,false,false,false,null);
        channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");
        Map<String, Object> arg = new HashMap<>();
        arg.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        arg.put("x-dead-letter-routing-key","lisi");
//        arg.put("x-message-ttl","100000");
        channel.queueDeclare(NORMAL_QUEUE,false,false,false,arg);
        channel.queueBind(NORMAL_QUEUE,NORMAL_EXCHANGE,"zhangsan");

        System.out.println("waitting message...");



        DeliverCallback deliverCallback=(consumerTag, message) -> {
            System.out.println(new String(message.getBody()));
        };
        channel.basicConsume(NORMAL_QUEUE,true,deliverCallback,consumerTag -> {});
        channel.basicConsume(DEAD_QUEUE,true,deliverCallback,consumerTag -> {});



    }
}
